package android.microblog.Abstract;

import java.util.SortedSet;

import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import android.app.Activity;
import android.content.Intent;
import android.microblog.Common.MicroblogType;
import android.microblog.Data.UserInfo;
import android.net.Uri;

public abstract class MicroblogOAuth {
	private CommonsHttpOAuthConsumer httpOauthConsumer;
	private OAuthProvider httpOauthprovider;
	public String consumer_key;
	public String consumer_secret;

	private String request_token_url;
	private String access_token_url;
	private String authorize_url;

	private MicroblogType type;

	public MicroblogOAuth(String consumerKey, String consumerSecret,
			MicroblogType type, String request_token_url,
			String access_token_url, String authorize_url) {
		this.consumer_key = consumerKey;
		this.consumer_secret = consumerSecret;
		this.type = type;
		this.request_token_url = request_token_url;
		this.access_token_url = access_token_url;
		this.authorize_url = authorize_url;
	}

	public Boolean RequestAccessToken(Activity activity, String callBackUrl) {
		Boolean ret = false;
		try {
			httpOauthConsumer = new CommonsHttpOAuthConsumer(consumer_key,
					consumer_secret);
			httpOauthprovider = new DefaultOAuthProvider(request_token_url,
					access_token_url, authorize_url);
			String authUrl = httpOauthprovider.retrieveRequestToken(
					httpOauthConsumer, callBackUrl);
			activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri
					.parse(authUrl)));
			ret = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}

	public UserInfo GetAccessToken(Intent intent) {
		UserInfo user = null;
		Uri uri = intent.getData();
		String verifier = uri
				.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
		try {
			httpOauthprovider.setOAuth10a(true);
			httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);
		} catch (OAuthMessageSignerException ex) {
			ex.printStackTrace();
		} catch (OAuthNotAuthorizedException ex) {
			ex.printStackTrace();
		} catch (OAuthExpectationFailedException ex) {
			ex.printStackTrace();
		} catch (OAuthCommunicationException ex) {
			ex.printStackTrace();
		}
		SortedSet<String> user_id = httpOauthprovider.getResponseParameters()
				.get("user_id");
		String userId = user_id.first();
		String token = httpOauthConsumer.getToken();
		String tokenSecret = httpOauthConsumer.getTokenSecret();
		user = new UserInfo();
		user.setUserId(userId);
		user.setType(type);
		user.setToken(token);
		user.setTokenSecret(tokenSecret);
		
		return user;
	}
}
